<?php
/*
 * File: reset.php
 * Created: 21 июля 2007
 * Author: Andrew Dashin <dash@andrewdashin.com>
 * 
 * Description:
 * 
 * This is reset password service.
 * 
 * Return values:
 * 
 * 0 Successfully logged in!
 * 1 Login is not defined!
 * 2 Email is not defined!
 * 3 There is no user with such Email and Login
 */
if (!isset($_POST['login']) or empty($_POST['login'])) {
	echo '1'; // Login is not defined!
} else {
	if (!isset($_POST['email']) or empty($_POST['email'])) {
		echo '2'; // Email is not defined!
	} else {
		
        $db = MySQL::getInstance();
		if (!$db->fetchAssoc(
            $db->sql("SELECT * FROM `users` WHERE `login` = ':0' AND `email` = ':1' AND `active` = 1 AND `email_confirmed` = 1")->bind($_POST['login'], $_POST['email'])
            )) {
			echo '3'; // There is no user with such Email and Login
		} else {
			// Send email with new password
            $newPassword = mt_rand(999999, 9999999);
            $db->sql("UPDATE `users` SET `password` = PASSWORD('{$newPassword}') WHERE `login` = ':0'")->bind($_POST['login']);
            mail($_POST['email'],
                "MyWebEr New Password",
                "Your new Password is: {$newPassword}",
                "From: donotreply@andrewdashin.com\r\nContent-type: text/plain; UTF-8\r\n");
            echo '0'; // Successfully logged in!
		}
    }
}
?>
